<template>
  <AcceptHandleHeadComponent
    ref="acceptHandleHeadRef"
    :ucolumns="ucolumns"
    :needDealNum="needDealNum"
    :type="LableTitle.notePad"
    :needMoreQuery="true"
    @myEvent="myEvent"
    @setColumns="setColumns"
    @onSearch="onSearch"
    @onReset="reQuery"
  >
    <!-- 其他查询条件 -->
    <div slot="more">
      <a-form layout="inline" class="accept_more_operate_moreForm">
        <a-collapse-panel :show-arrow="false" disabled key="1">
          <div slot="header" class="accept_more_operate_moreForm_collapse_head">
            <a-row :gutter="48" style="width: 100%">
              <a-col @click.stop="() => {}" :md="8" :sm="24">
                <a-form-item label="申请人：">
                  <a-input v-model="queryParam.f_applyname" placeholder="请输入申请人" allowClear />
                </a-form-item>
              </a-col>
              <a-col @click.stop="() => {}" :md="8" :sm="24">
                <a-form-item label="被申请人：">
                  <a-input v-model="queryParam.f_targetname" placeholder="请输入被申请人" allowClear />
                </a-form-item>
              </a-col>
            </a-row>
          </div>
        </a-collapse-panel>
      </a-form>
    </div>

    <template slot="content">
      <div class="accept_content_table">
        <!-- 表格数据 -->
        <a-table
          :scroll="scroll"
          :columns="ucolumns"
          :data-source="listData"
          :pagination="upagination"
          :loading="loading"
          rowKey="id"
          @change="tableChange"
          size="middle"
          :bordered="ubordered"
          :rowClassName="rowClassName"
          :components="header"
          ref="table"
          :key="tableCountKey"
        >
          <span slot="serial" slot-scope="text, record, index">
            {{ (upagination.current - 1) * upagination.pageSize + parseInt(index) + 1 }}
          </span>

          <div slot="statusSlot" slot-scope="text, record" class="beyond_hiding" style="width: 100%">
            <a-tooltip placement="topLeft">
              <template slot="title">
                <span>{{ record.status in St_apply ? St_apply[record.status].name : '' }}</span>
              </template>
              {{ record.status in St_apply ? St_apply[record.status].name : '' }}
            </a-tooltip>
          </div>
          <div slot="acceptcodeSlot" slot-scope="text, record" class="beyond_hiding" style="width: 100%">
            <a-tooltip placement="topLeft">
              <template slot="title">
                <span>{{ record.acceptcode }}</span>
              </template>
              {{ record.acceptcode }}
            </a-tooltip>
          </div>

          <div
            slot="applynameSlot"
            slot-scope="text, record"
            class="beyond_hiding"
            style="width: 100%; text-align: left"
          >
            <a-popover placement="topLeft">
              <template slot="title">
                <span>{{ record.applyname }}</span>
              </template>
              {{ record.applyname }}
            </a-popover>
          </div>

          <div
            slot="targetnameSlot"
            slot-scope="text, record"
            class="beyond_hiding"
            style="width: 100%; text-align: left"
          >
            <a-popover placement="topLeft">
              <template slot="title">
                <span>{{ record.targetname }}</span>
              </template>
              {{ record.targetname }}
            </a-popover>
          </div>

          <div slot="createdateSlot" slot-scope="text, record" class="beyond_hiding" style="width: 100%">
            <a-tooltip placement="topLeft">
              <template slot="title">
                <span>{{ record.createdate }}</span>
              </template>
              {{ record.createdate }}
            </a-tooltip>
          </div>

          <span slot="operation" slot-scope="text, record">
            <!-- 详情 -->
            <a @click="showHand(record, '4', false)" style="display: inline-block">详情</a>

            <!-- 这是一个垂直分隔线 -->
            <a-divider type="vertical" />

            <!-- 这是你之前的 NotePadModal -->
            <NotePadModal :applyid="record.applyid" style="display: inline-block"></NotePadModal>
          </span>
        </a-table>
      </div>
    </template>
  </AcceptHandleHeadComponent>
</template>

<script>
import drag from '@/components/TableDrag/drag'
import { St_admreview } from '@/enumeration/St_admreview'
import { LableTitle } from '@/enumeration/LableTitle.js'
import { Tp_biztype } from '@/enumeration/Tp_biztype'
import { Tp_admpr } from '@/enumeration/Tp_admpr'
import { Tp_apptype, Tp_apptypeList } from '@/enumeration/Tp_apptype.js'
import { notebooklist, removenotebook } from '@/api/apply/apl_note.js'
import { APP_USER, APP_DEPT } from '@/store/mutation-types.js'
import storage from 'store'
import AcceptHandleHeadComponent from '@/views/accept/component/AcceptHandleHeadComponent.vue'
import { St_apply } from '@/enumeration/St_apply.js'
import { queryLastTaskidByInstid } from '@/api/archive/apl_archive'
import { getViewTaskByInstid } from '@/api/worktasks/wt_personal.js'

const ucolumns = [
  {
    title: '序号',
    width: 50,
    key: 'index',
    align: 'center',
    fixed: 'left',
    scopedSlots: { customRender: 'serial' }
  },
  {
    title: '状态',
    width: 120,
    dataIndex: 'status',
    align: 'center',
    scopedSlots: { customRender: 'statusSlot' }
  },
  {
    title: '案号',
    width: 300,
    dataIndex: 'acceptcode',
    align: 'center',
    scopedSlots: { customRender: 'acceptcodeSlot' }
  },
  {
    title: '申请人',
    dataIndex: 'applyname',
    width: 155,
    align: 'center',
    scopedSlots: { customRender: 'applynameSlot' }
  },
  {
    title: '被申请人',
    width: 360,
    dataIndex: 'targetname',
    align: 'center',
    scopedSlots: { customRender: 'targetnameSlot' }
  },

  {
    title: '最后记录时间',
    dataIndex: 'createdate',
    width: 300,
    align: 'center',
    scopedSlots: { customRender: 'createdateSlot' }
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: 300,
    fixed: 'right',
    align: 'center',
    scopedSlots: { customRender: 'operation' }
  }
]

export default {
  data () {
    return {
      applyid: null,
      instid: '',

      isEntrustModel: false, // 分派任务树
      ucolumns: ucolumns,
      // columns: columns,
      cardColumns: ucolumns,
      needDealNum: undefined,
      queryParam: {
        f_status: '', //状态
        f_acceptcode: '', //案号
        f_applyname: '', //申请人
        f_targetname: '', //被申请人
        f_createdate: '' //最后记录时间
      },

      listData: [],
      upagination: {
        // 表格分页参数
        defaultPageSize: 20,
        total: 0,
        current: 1,
        pageSize: 20,
        showTotal: total => `共计 ${total} 条数据`,
        showSizeChanger: true,
        pageSizeOptions: ['10', '20', '50', '100']
      },
      loading: false,
      ubordered: true, // 表格边框是否显示
      notepadpopup: false, //记事本弹窗是否显示

      mixins: [drag],
      St_admreview: St_admreview,
      Tp_biztype: Tp_biztype,
      Tp_admpr: Tp_admpr,
      currentUser: storage.get(APP_USER), // 登录人信息
      currentDept: storage.get(APP_DEPT), // 登录人部门
      sourceType: [],
      LableTitle: LableTitle,
      header: undefined,
      tableCountKey: 0,
      collapseKey: [], // 折叠面板key
      Tp_apptypeList: Tp_apptypeList,
      St_apply: St_apply,
      scroll: { x: 1000, y: 480 }
    }
  },

  components: {
    BusineUserTree: () => import('@/views/accept/BusineUserTree.vue'),
    AcceptHandleHeadComponent,
    NotePadModal: () => import('@/views/base/NotePadModal.vue')
  },
  created () {
    //console.log('this.$route.query.queryParam.queryList', this.$route.query.queryParam)

    try {
      //routeParam = JSON.parse(JSON.stringify(this.$route.query.queryParam))
      this.queryParam = { ...this.queryParam, ...this.$route.query.queryParam }
    } catch {
      console.log('路由传参非json格式')
    }

    this.headerChange()
    // 待处理人员s
    this.initDefaultParam()
    this.queryUndoTaskList()
  },
  watch: {
    ucolumns: {
      handler (newValue, oldVal) {
        this.headerChange()
      },
      deep: true // 深度监听
    }
  },
  methods: {
    myEvent (e) {
      if (e) {
        this.scroll.y = this.scroll.y + e
      } else {
        this.scroll.y = 480
      }
    },
    // 进入申报页面

    headerChange () {
      console.log('移动后', this.ucolumns)
      this.header = {
        header: {
          cell: drag.methods.initDrag(this.ucolumns)
        }
      }
    },
    setColumns (columns) {
      console.log('重置前', this.ucolumns)
      // tableCountKey自增是为了让表格控件每次销毁强制更新
      this.tableCountKey += 1
      this.ucolumns = columns
      // this.ucolumns = columns
      console.log('重置后', this.ucolumns)
      this.setCardColumns()
    },
    onSearch (val) {
      this.queryParam.f_acceptcode = val
      this.queryParam.pageNumber = 1
      this.queryParam.pageSize = 20

      this.queryList()
    },

    // 接收时间监听判断开始结束日期是否合理
    subtimeListen () {
      if (
        this.queryParam.applyStartDate > this.queryParam.applyEndDate &&
        this.queryParam.applyStartDate &&
        this.queryParam.applyEndDate
      ) {
        this.queryParam.applyStartDate = undefined
      }
    },
    /**
     * 初始化设置默认值
     */
    initDefaultParam () {
      this.currentUser = storage.get(APP_USER)
    },
    /**
     * 重置查询条件并查询数据
     */
    reQuery () {
      this.queryParam = {
        f_status: '', //状态
        f_acceptcode: '', //案号
        f_applyname: '', //申请人
        f_targetname: '', //被申请人
        f_createdate: '' //最后记录时间
      }

      this.initDefaultParam()
      this.queryUndoTaskList()
    },

    tableChange (upagination, filters, sorter, { currentDataSource }) {
      this.row_idx = upagination.current * upagination.pageSize - upagination.pageSize
      this.queryParam.pageNumber = upagination.current
      this.queryParam.pageSize = upagination.pageSize
      this.queryUndoTaskList()
    },
    tableChangeOther (upagination) {
      this.row_idx = upagination.current * upagination.pageSize - upagination.pageSize
      this.queryParam.pageNumber = upagination.current
      this.queryParam.pageSize = upagination.pageSize
      this.queryUndoTaskList()
    },
    queryUndoTaskList () {
      this.queryList()
    },

    // 状态转换显示
    statusLable (record) {
      const val = record.status
      for (const key in this.St_apply) {
        if (this.St_apply[key].value == val) {
          return this.St_apply[key].name
        }
      }
      return val
    },

    getCpQueryParam () {
      // 时间转换
      const cpQueryParam = JSON.parse(JSON.stringify(this.queryParam))
      if (this.queryParam.f_createdate?.format) {
        cpQueryParam.f_createdate = this.queryParam.f_createdate.format('YYYY-MM-DD') + ' 00:00:00'
      }
      if (this.queryParam.f_updatedate?.format) {
        cpQueryParam.f_updatedate = this.queryParam.f_updatedate.format('YYYY-MM-DD') + ' 23:59:59'
      }
      return cpQueryParam
    },
    //查询 记事本信息
    queryList () {
      const queryParam = this.getCpQueryParam()
      if (queryParam.f_applyname) {
        queryParam.f_applyname = this.queryParam.f_applyname.toString().trim()
      }
      if (queryParam.f_targetname) {
        queryParam.f_targetname = this.queryParam.f_targetname.toString().trim()
      }
      this.loading = true
      notebooklist(queryParam)
        .then(res => {
          if (res.code !== 0) {
            this.$message.error('获取数据失败')
          } else {
            this.listData = res.data.records
            this.upagination.total = res.data.total
            this.upagination.current = res.data.current
            this.upagination.pageSize = res.data.size
            this.needDealNum = res.data.total
          }
        })
        .catch(err => {
          this.listData = []
          console.log(err, '获取数据失败')
        })
        .finally(() => {
          this.loading = false
        })
    },
    //记事本弹窗
    notepad () {
      this.notepadpopup = true
    },

    /**
     * 获取流程任务id
     */
    getTaskId (instid, callBack) {
      queryLastTaskidByInstid({ processinstanceid: instid }).then(res => {
        if (res.code !== 0) {
          this.$message.error('获取流程实例错误')
        } else {
          const taskid = res.data
          callBack(taskid)
        }
      })
    },

    // /**
    //  * 显示详情界面
    //  */
    // showHand (record, pattern, isEditVal) {
    //   console.log('record', record)
    //   // viewApl_main
    //   this.getTaskId(record.instid, taskid => {
    //     record.isShowArchive = true
    //     record.isExecute = true
    //     record.isExecuteEdit = false
    //     record.taskid = taskid
    //     record.pattern = pattern
    //     record.isEditVal = isEditVal
    //     record.isDocEditVal = isEditVal
    //     // 将record存入localstorage
    //     const storageData = JSON.stringify(record)
    //     localStorage.setItem('archiveData', storageData)

    //     this.$router.push({
    //       path: `/ArriveInfoHandleOperation/${taskid}/4`,
    //       query: {
    //         url: '/NoteBook',
    //         queryParam: record
    //       }
    //     })
    //   })
    // },
    // 查看操作
    async showHand (record) {
      const { data } = await getViewTaskByInstid(record.instid)
      if (data == null) {
        this.$message.error('未找到taskid')
        return
      }
      this.$router.push({
        path: `/CompreQueryHandleOperation/${data}/4`,
        query: {
          url: '/NoteBook',
          queryParam: this.queryParam
        }
      })
      console.log('this.queryParam', this.queryParam)
      console.log('this.$route.query.queryParam', this.$route.query.queryParam)
    },
    //删除记事本信息
    async removebyId (record) {
      console.log('record', record.id)
      let res = await removenotebook(record.id).finally(() => {
        this.loading = false
      })
      if (res.code != 0) {
        this.$message.error('删除失败')
      } else {
        this.$message.success('删除成功')
      }
      this.queryList()
    },

    rowClassName (record, index) {
      return index % 2 === 0 ? 'oddRow' : 'evenRow'
    },

    setCardColumns () {
      const columns = this.ucolumns.filter(child => child.key != 'index' && child.key != 'action')
      this.cardColumns = JSON.parse(JSON.stringify(columns))
    }
  }
}
</script>

<style lang="less" scoped>
@import '~@/components/TableDrag/style.less';
</style>

<style lang="scss" scoped>
@import '~@/assets/css/apply/breathingLamp.scss';

.cursor_pointer:hover {
  color: #55a7f5;
}

.accept_content {
  &_table {
    &_font_red {
      color: #ca0000;
      font-size: 16px;
      font-family: 'PingFang-SC-Heavy';
      font-weight: 400;
    }

    ::v-deep .ant-table-row-cell-break-word {
      font-size: 16px;
      font-family: 'PingFang-SC-Heavy';
      font-weight: 400;
      color: #2c2c2c;
    }

    ::v-deep .ant-table-thead > tr > th {
      height: 50px;
    }

    ::v-deep .ant-table-row-cell-break-word {
      font-size: 16px;
      font-family: 'PingFang-SC-Heavy';
      font-weight: 600;
      color: #2c2c2c;
    }

    // ::v-deep .ant-pagination-total-text {
    //   font-size: 16px;
    //   font-family: 'PingFang-SC-Heavy';
    //   font-weight: 400;
    //   color: #8e97b9;
    // }

    ::v-deep .ant-table-body,
    .ant-table-header {
      word-break: break-all;

      &::-webkit-scrollbar {
        width: 10px;
        /*高宽分别对应横竖滚动条的尺寸*/
        // height: 10px;
      }

      &::-webkit-scrollbar-thumb {
        border-radius: 6px;
        // -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
        background: #a2c6e1;
      }

      &::-webkit-scrollbar-track {
        // -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
        border-radius: 5px;
        background: transparent;
      }
    }
  }
}
</style>
